#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <math.h>

int reduce(char* big_n) {
	int t = 0;
	for (int i = 0; i < strlen(big_n); i++) {
		t += big_n[i] - '0';
	}
	return t;
}

int main() {
	int n;
	scanf("%d", &n);
	char* buffer = (char*) malloc(1000200);

	while (n-- > 0) {
		scanf("%1000100s", buffer);

		int r1 = reduce(buffer);
		
		putchar('0' + (r1 - 1) % 9 + 1);
		putchar('\n');
	}

	free(buffer);
}
